package cheme.ProcessModel.Visitors;

import cheme.ProcessModel.IParent;
import cheme.ProcessModel.Equations.Equation;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.AbstractTagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.OverallTagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.Tag;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.TagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.TagComponentList;
import cheme.ProcessModel.Table.Field;

public class RenderTextVisitor extends ReflectiveVisitor
{
	private static RenderTextVisitor instance = null;
	
	private RenderTextVisitor()
	{
	}
	
	public static RenderTextVisitor getInstance()
	{
		if(null == instance)
		{
			instance = new RenderTextVisitor();
		}
		
		return instance;
	}
	
	public String visit(ISubject subject)
	{
		return (String)super.visit(subject);
	}
	
	public String defaultVisit(ISubject subject)
	{
		return subject.toString();
	}
	
	String visitChildren(IParent parent)
	{
		StringBuilder builder = new StringBuilder();
		
		for(Object child : parent.getChildren())
		{
			builder.append(visit((ISubject)child));
		}
		
		return builder.toString();
	}
	
	public String visit(Tag tag)
	{
		return visitChildren(tag);
	}
	
	public String visit(TagComponentList tagComponentList)
	{
		StringBuilder builder = new StringBuilder();
		
		for(Object child : tagComponentList.getChildren())
		{
			builder.append(visit((ISubject)child));
		}

		builder.append("\n");
		
		return builder.toString();
	}
	
	public String visit(OverallTagComponent tagComponent)
	{
		return visitTagInternal(tagComponent) + "\n";
	}

	public String visit(TagComponent tagComponent)
	{
		StringBuilder builder = new StringBuilder();
		
		builder.append(" > " + visitTagInternal(tagComponent));
		builder.append(" ");
		builder.append(tagComponent.getMaterial());
		builder.append("\n");
		
		return builder.toString();
	}
	
	public String visitTagInternal(AbstractTagComponent tagComponent)
	{
		StringBuilder builder = new StringBuilder();
				
		builder.append(tagComponent.getLabel());
		builder.append(" : ");
		
		{
			String quantity = tagComponent.getQuantity();

			if(!quantity.isEmpty())
			{
				builder.append(quantity);
			}
			else
			{
				builder.append("?");
			}
		}
		
		builder.append(" ");
		builder.append(tagComponent.getUnits());
		
		return builder.toString();
	}
	
	public String visit(Equation equation)
	{
		StringBuilder builder = new StringBuilder();
		
		for(Field field : equation.getChildren())
		{
			builder.append(field.getText());
			builder.append(" ");
		}

		return builder.toString();
	}
	
	public String visit(Field field)
	{
		return field.getText();
	}
}
